package org.gitos.cloud.server.service;

import java.util.List;

import org.gitos.cloud.domain.server.Rom;
import org.gitos.cloud.util.CommonKit;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.stereotype.Component;
import org.springframework.util.StringUtils;

/**
 * Created by hc on 2016/3/30 0030. app消息推送
 */
@Component
public class RomService {
	private static Logger log = LoggerFactory.getLogger(RomService.class);

	@Autowired
	private JdbcTemplate jdbcTemplate;

	public void addRom(String name, String version, int type, String url) {
		jdbcTemplate.update("insert into rom (uuid,name,version,type,url,created) values (?,?,?,?,?,now())", CommonKit.createUuid(), name, version, type, url);
	}

	public List<Rom> list(int pageNumber, int pageSize, int type, String sortName, String sortOrder) {
		String orderBy = "order by ";
		if (StringUtils.hasLength(sortName)) {
			orderBy += sortName;
			if (StringUtils.hasLength(sortOrder)) {
				orderBy += " " + sortOrder;
			}
		} else {
			orderBy += "created desc";
		}
		return jdbcTemplate.query("select * from rom where type=? limit ?,? " + orderBy, new Object[] { type, pageNumber * pageSize, pageSize }, new Rom());
	}

	public List<Rom> list(int pageNumber, int pageSize, int type) {
		return jdbcTemplate.query("select * from rom where type=? limit ?,? order by created desc", new Object[] { type, pageNumber * pageSize, pageSize },
				new Rom());
	}
}
